package com.sugar.web.struts.oa.hr;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sugar.base.query.PageView;
import com.sugar.base.util.ParseArrayUtil;
import com.sugar.domain.oa.hr.Employee;
import com.sugar.service.oa.hr.IDepartmentService;
import com.sugar.service.oa.hr.IEmployeeService;
import com.sugar.service.system.privilege.IPrivilegeRoleService;
import com.sugar.web.struts.BaseActionSupport;

@Controller
@Scope("prototype")
public class EmployeeAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	private Employee emp;
	
	private String delIds;

	private String type ;
	
	private String empids ;
	
	private String roleids ;
	
	private int[] rids ;
	
	@Resource
	private IEmployeeService emps;

	@Resource
	private PageView<Employee> pageView;

	@Resource
	private IDepartmentService deptService ;

	@Resource
	private IPrivilegeRoleService roleService ;

	public String addEmpAction() {

		this.emps.saveEmpService(emp);

		return "addEmpSuccess";
	}

	/**
	 * 为员工分配资源（批量）
	 * @return
	 * @throws Exception
	 */
	public String assignPrivilegeMoreEmp() throws Exception {
		
		this.emps.updateAssignRoleToEmpService(empids, roleids) ;
		
		return "assignEmp" ;
	}

	/**
	 * 单独为员工添加角色
	 * @return
	 * @throws Exception
	 */
	public String assignRoleToSelfEmp() throws Exception {
		this.emps.updateAssignRoleSelfEmp(empids, rids) ;
		
		return "roleToSelf" ;
	}

	public String deleteSelectempAction() {

		this.emps.deleteSelectEmpService(ParseArrayUtil.parseArray(delIds));

		return "deleteSelectSuccess";
	}
	
	public String findEmpAction() {

		getRequest().setAttribute("emp", this.emps.findEmpService(null));

		return SUCCESS;
	}
	
	public String getDelIds() {
		return delIds;
	}

	public Employee getEmp() {
		return emp;
	}
	
	public String getEmpAction() {
		
		getRequest().setAttribute("emp", this.emps.loadEmpByIdService(emp.getEmpNo())) ;
		
		if("update".equals(type)) {
			return "updatePage" ;
		} else if("role".equals(type)) {
			getRequest().setAttribute("role", this.roleService.findPrivilegeRoleService(null)) ;
			return "bySelfRole" ;
		}else {
			return "get" ;
		}
	}
	
	public String getEmpids() {
		return empids;
	}
	
	public PageView<Employee> getPageView() {
		return pageView;
	}

	public int[] getRids() {
		return rids;
	}

	public String getRoleids() {
		return roleids;
	}

	public String getScrollEmpAction() {

		this.pageView.setPageModel(this.emps.getScorllEmpService(null,
				this.pageView.getFirstResult(), this.pageView.getMaxResult()));

		getRequest().setAttribute("pageView", pageView);

		return "getScrollSuccess";
	}

	public String getType() {
		return type;
	}

	public String loadDeptRole() throws Exception {
		this.getRequest().setAttribute("dept", this.deptService.findDeptService(null)) ;
		getRequest().setAttribute("role", this.roleService.findPrivilegeRoleService(null)) ;
		
		return "loadDeptRole" ;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}
	
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
	
	public void setEmpids(String empids) {
		this.empids = empids;
	}

	public void setPageView(PageView<Employee> pageView) {
		this.pageView = pageView;
	}

	public void setRids(int[] rids) {
		this.rids = rids;
	}

	public void setRoleids(String roleids) {
		this.roleids = roleids;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String updateEmpAction() {
		
		this.emps.updateEmpService(emp) ;
		
		return "update" ;
	}

}
